import { _decorator, Component, Node, EventHandler, Enum } from 'cc';
import { EDITOR } from 'cc/env';
import { KeyActionHandler } from './KeyActionHandler';
import { KeyAction, KeyActionInfo, KeyActionType } from './KeyActionDefine';
import { KeyCodeHandler } from '../base/KeyCodeHandler';
import Debug from '../../../tools/Debug';
import { InputKeyCode } from '../../core/InputConfig';
import GameInputMgr from '../../core/GameInputMgr';
const { ccclass, property } = _decorator;

@ccclass('MoveActionHandler')
export class MoveActionHandler extends KeyActionHandler {



    @property({ type: EventHandler, tooltip: "上" })
    up: EventHandler = new EventHandler()
    @property({ type: EventHandler, tooltip: "左" })
    left: EventHandler = new EventHandler()
    @property({ type: EventHandler, tooltip: "右" })
    right: EventHandler = new EventHandler()
    @property({ type: EventHandler, tooltip: "下" })
    down: EventHandler = new EventHandler()

    onLoad() {

    }
    start() {
        this.initHandlers()
        super.start();
    }

    initHandlers() {
        if (EDITOR) {
            this.removeAllKeyCodeHandler();
            return;
        }
        if (this.kcHandlers.length > 0) {
            return;//已经初始化过了
        }
        Debug.log('addHandlers  this.isContinuous  ', this.isContinuous)
        this._actionType = KeyActionType.move;
        let data: KeyAction = KeyActionInfo[KeyActionType[this._actionType]]
        let list = data.keyCodes;
        let keyCodes: InputKeyCode[] = []
        // let handlers = this.node.getComponents(KeyCodeHandler)
        // while (handlers.length > 0) {
        //     let element = handlers.shift();
        //     element.destroy();
        // }
        for (let j = 0; j < list.length; j++) {
            let code = GameInputMgr.instance().converter.getKeyValueByKeyName(list[j])
            keyCodes.push(code);
            let kcHandler = this.node.addComponent(KeyCodeHandler)
            kcHandler.enabled = true;
            kcHandler.isContinuous = this.isContinuous;
            kcHandler.continuousDuration = this.continuousDuration;
            kcHandler.isBelongKeyBoard = this.isBelongKeyBoard;
            kcHandler.keyCode = code;
            if (this.model) {
                kcHandler.soundEventId = this.model.soundEventId;
            }
            switch (code) {
                case InputKeyCode.LEFT:
                    kcHandler.handler = this.left;
                    break;
                case InputKeyCode.UP:
                    kcHandler.handler = this.up;
                    break;
                case InputKeyCode.RIGHT:
                    kcHandler.handler = this.right;
                    break;
                case InputKeyCode.DOWN:
                    kcHandler.handler = this.down;
                    break;
            }
            this.kcHandlers.push(kcHandler);
        }

        // kcHandler.keyCode = this.keyCodes[i];
        // kcHandler.handler = this.handler[i]

    }
    update(deltaTime: number) {

    }
}

